// pages/page/goods_details/goods_details.js
import Toast from '../../../youzan/youzan_dist/toast/toast';
var request_api = require('../../../utils/require_api.js')
const app = getApp()
Page({
  data: {
    active: 0,
    pic_number: 1,
    pic_middle: 1,
    winWidth: 0,
    winHeight: 0,
    winHeight1: 0,
    Spec_parameter: {}, //获取规格参数信息
    currentTab: 0,
    classes_array: [],
    swiperCurrent: 1,
    show_picture: '', //点击弹出框选择颜色和尺寸时候的款图片
    cartlength: 0, //获取到的购物车数量
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    circular: true,
    beforeColor: "white", //指示点颜色  
    afterColor: "coral", //当前选中的指示点颜色  
    previousmargin: '30px', //前边距
    nextmargin: '30px', //后边距
    show: false, //控制弹出层的显示
    lunbo_pic: [], //轮播图片
    pic_details: [], //详情图片
    data_show: [], //获取某一款号的基本信息，用于显示在界面上
    color_pic: [],
    color_arr: [], //
    datalist: [], //获取某一款总的库存表列表数组
    onclickbutton: true,
    size_select: [{
        name: 'S',
        id: 'S',
        checked: false
      }, {
        name: 'M',
        id: 'M',
        checked: false
      },
      {
        name: 'L',
        id: 'L',
        checked: false
      }
    ], //尺码选择数组
    color_show: '', //点击颜色选择的颜色id
    size_show: '', //点击尺码选择的尺码数
    stock_number: '0', //用于显示库存数
    number_select: 1,
    good_id: '',
  },

  //轮播图的切换事件  
  swiperChange: function(e) {
    this.setData({
      swiperCurrent: e.detail.current //获取当前轮播图片的下标
    })
  },
  //滑动图片切换  
  chuangEvent: function(e) {
    this.setData({
      swiperCurrent: e.currentTarget.id
    })
  },
  swiperChange: function(e) {
    this.setData({
      swiperCurrent: e.detail.current //获取当前轮播图片的下标
    })
  },
  //购物车的图标点击按钮
  onClickIcon() {
    wx.switchTab({
      url: '../cart/cart',
    })
  },
  //购物车的按钮触发和立即购买触发
  onClickButton() {
    this.setData({
      show: true
    });
    this.onclickbutton = true;
    // this.data.
  },
  //立即购买
  goshopButton() {
    this.setData({
      show: true
    });
    this.onclickbutton = false;
  },

  //加入购物车时候点击关闭触发
  onClose() {
    this.setData({
      show: false
    });
  },
  confirm_click() {
    var that = this;
    var filed = '';
    var size_class = '';
    if (this.size_show == 'S') {
      filed = 'S1';
      size_class = '02';
    } else if (this.size_show == 'M') {
      filed = 'S2';
      size_class = '02';
    } else if (this.size_show == 'L') {
      filed = 'S3';
      size_class = '02';
    }

    if (!this.onclickbutton) {
      //直接购买
      if (this.data.stock_number == 0) {
        Toast('该款颜色尺码的库存为0,请重新选择！！！');
      } else {
        console.log(this.data.good_id, "你哈");
        console.log(size_class, "你哈");
        console.log(this.color_show, "你哈");
        console.log(filed, "你哈");
        console.log(this.data.number_select, "你哈");
  var jsonobj={
    goods: this.data.good_id,
    size_class: size_class,
    color_show: this.color_show,
    filed: filed,
    number_select: this.data.number_select
  }
        var data = JSON.stringify(jsonobj);
        console.log(data,"addda")
        wx.navigateTo({
          url: '../orders/orders?jsonobj=' + data,
        })
      }
    } else {
      //购物车
      // console.log(app.globalData.openid, "你哈");
      // console.log(size_class, "你哈");
      // console.log(this.color_show, "你哈");
      // console.log(filed, "你哈");
      // console.log(this.data.number_select, "你哈");
      if (this.data.stock_number == 0) {
        Toast('该款颜色尺码的库存为0，添加购物车失败');
      } else {
        request_api.request(
          'get_ShoppingCartifexist', //需要传给wxrequest.js 的 url
          {
            "u_id": app.globalData.openid,
            "goods_id": this.data.good_id,
            "Filed": filed,
            "size_class": size_class,
            "colorId": this.color_show,
            "num": this.data.number_select
          }, //需要传的 data
          function(res) {
            //查看是否存在相同數據
            if (res.data != '') {
              //"修改数量"
              request_api.request(
                'update_shoppingcart', //需要传给wxrequest.js 的 url
                {
                  "u_id": app.globalData.openid,
                  "goods_id": that.data.good_id,
                  "Filed": filed,
                  "size_class": size_class,
                  "colorId": that.color_show,
                  "num": that.data.number_select + res.data.num
                }, //需要传的 data
                function(res) {
                  that.getcartlength();
                  Toast('增加成功！！！');
                  that.setData({
                    show: false
                  });
                },
                function() {
                  console.log("錯誤");
                },
              )
            } else {
              //增加一条记录
              request_api.request(
                'insert_Shoppingcart', //需要传给wxrequest.js 的 url
                {
                  "u_id": app.globalData.openid,
                  "goods_id": that.data.good_id,
                  "Filed": filed,
                  "size_class": size_class,
                  "colorId": that.color_show,
                  "num": that.data.number_select
                }, //需要传的 data
                function(res) {
                  Toast('增加成功！!!');
                  that.getcartlength();
                  that.setData({
                    show: false
                  });
                },
                function() {
                  console.log("錯誤");
                },
              )
            }
          },
          function() {
            console.log("錯誤");
          },
        )
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({
      success: function(res) {
        that.data.winHeight1 = res.windowHeight;
        that.setData({
          winWidth: res.windowWidth,
          winHeight: 515
        });
      }
    });
    this.data.good_id = options.style_id;
    request_api.request(
      'goods_OneDetails', //需要传给wxrequest.js 的 url
      {
        "parameter": options.style_id
      }, //需要传的 data
      function(res) {
        if (res.data[0] != undefined) {
          that.setData({
            data_show: res.data[0]
          })
        } else {
          that.setData({
            data_show: ''
          })
        }
      },
      function() {
        console.log("錯誤");
      },
    )

    request_api.request( //获取规格参数信息
      'get_spec_parameter', {
        "goods_no": options.style_id
      }, //需要传的 data
      function(res) {
        that.setData({
          Spec_parameter: res.data
        })
      },
      function() {
        console.log("錯誤");
      },
    )

    //  获取颜色尺码
    request_api.request(
      'get_Color_code', //需要传给wxrequest.js 的 url
      {
        "parameter": options.style_id
      }, //需要传的 data
      function(res) {
        that.datalist = res.data;
        for (let i = 0; i <= res.data[0].length - 1; i++) {
          if (that.data.color_arr.indexOf(res.data[0][i].color_name) === -1) {
            that.data.color_arr.push({
              "name": res.data[0][i].color_name,
              "id": res.data[0][i].color_Id,
              "checked": false
            });
          }
        }
        var hash = {};
        that.data.color_arr = that.data.color_arr.reduce(function(item, next) {
          hash[next.name] ? '' : hash[next.name] = true && item.push(next);
          return item
        }, [])
        that.setData({
          classes_array: that.data.color_arr
        })

      },
      function() {
        console.log("錯誤");
      },
      //获取该id所有的颜色并且获取图片
      request_api.request(
        'get_styleidcolor', //需要传给wxrequest.js 的 url
        {
          "style_id": options.style_id
        }, //需要传的 data
        function(res) {
          that.data.color_pic = res.data
          if (res.data[0].pic === undefined) {
            that.setData({
              show_picture: ''
            })
          } else {
            that.setData({
              show_picture: that.data.color_pic[0].pic
            })
          }
        },
        function() {
          console.log("錯誤");
        },
      )
    )
    //获取到款详情图片
    this.data.lunbo_pic = ['', '', '', ''];
    request_api.request(
      'goods_pic', //需要传给wxrequest.js 的 url
      {
        "parameter": options.style_id
      }, //需要传的 data
      function(res) {
        var lunbotu = [];
        var xiangqingtu = [];
        that.data.pic_number = 0;

        if (res.data.length != 0) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].style == 1) {
              lunbotu.push(res.data[i].url);
            } else if (res.data[i].style == 2) {
              that.data.pic_number = that.data.pic_number + 1;
              xiangqingtu.push(res.data[i].url);
            }
          }
        }
        that.data.pic_middle = that.data.pic_number;
        that.setData({
          pic_details: xiangqingtu,
          lunbo_pic: lunbotu,
          pic_number: that.data.pic_number
        })
      },
      function() {
        console.log("錯誤");
      },
    )
    //获取购物车数量
    this.getcartlength();
  },
  //点击radio-group中的列表项事件(颜色)
  radiochange: function(res) {
    console.log(res)
    for (let i = 0; i < this.data.color_pic.length; i++) {
      if (res.detail.value == this.data.color_pic[i].ColorID) {
        if (this.data.color_pic[i].pic != undefined) {
          this.setData({
            show_picture: this.data.color_pic[i].pic
          })
        }
      }
    }

    var stock = '';
    this.color_show = res.detail.value;
    for (let i = 0; i < this.datalist[0].length; i++) {
      if (this.color_show == this.datalist[0][i].colorID && this.size_show == this.datalist[0][i].short_size) {
        stock = this.datalist[0][i].quantity
      }
    }
    if (stock == '') {
      stock = '0';
    }
    var arrs = this.data.classes_array;
    var that = this;
    for (const x in arrs) {
      if (arrs[x].id == res.detail.value) {
        arrs[x].checked = true;
      } else {
        arrs[x].checked = false;
      }
    }
    that.setData({
      classes_array: arrs,
      stock_number: stock,
      number_select: 1
    })
  },

  //点击按钮(尺码)
  radiochangeSize: function(res) {
    var stock = '';
    this.size_show = res.detail.value;
    for (let i = 0; i < this.datalist[0].length; i++) {
      if (this.color_show == this.datalist[0][i].colorID && this.size_show == this.datalist[0][i].short_size) {
        stock = this.datalist[0][i].quantity == '' ? "0 " : this.datalist[0][i].quantity;
      }
    }
    if (stock == '') {
      stock = '0';
    }
    var arrs = this.data.size_select;
    var that = this;
    for (const x in arrs) {
      if (arrs[x].id == res.detail.value) {
        arrs[x].checked = true;
      } else {
        arrs[x].checked = false;
      }
    }
    that.setData({
      size_select: arrs,
      stock_number: stock,
      number_select: 1
    })
  },

  //数量控件改变触发
  onChange_number(event) {
    this.data.number_select = event.detail;
  },


  bindChange: function(e) {
    if (e.detail.current == 1) {
      this.setData({
        winHeight: this.data.winHeight1 - 100,
        pic_number: 1
      })
    } else {
      this.setData({
        winHeight: 515,
        pic_number: this.data.pic_middle
      })
    }
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  /** 
   * 点击tab切换 
   */
  swichNav: function(e) {

    var that = this;
    console.log(that.data.pic_number, " console.log(that.data.pic_number)");
    if (e.target.dataset.current == 1) {
      this.setData({
        winHeight: this.data.winHeight1 - 100,
        pic_number: 1
      })
    } else {
      this.setData({
        winHeight: 515,
        pic_number: this.data.pic_middle
      })
    }
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  onChange(event) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //获取购物车数量
  getcartlength: function() {
    var that = this;
    console.log(that.data.good_id);
    request_api.request(
      'shoppongcartAllData', //需要传给wxrequest.js 的 url
      {
        uid: app.globalData.openid
      }, //需要传的 data
      function(res) {
        console.log(res.data.length, "www")
        console.log(res)
        that.setData({
          cartlength: res.data.length
        })
      },
      function() {
        console.log("錯誤");
      },
    )
  },
})